package com.fly.caihong.frag;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fly.caihong.R;
import com.fly.caihong.activity.AuctionDetailActivity;
import com.fly.caihong.adapter.HomeAuctionAdapter;
import com.fly.caihong.base.BasePageListFragment;
import com.fly.caihong.model.BannerItem;
import com.fly.caihong.model.CategoryItem;
import com.fly.caihong.model.HomeAuctionItem;
import com.fly.caihong.utils.ApUtils;
import com.fly.caihong.utils.DataHandler;
import com.fly.caihong.utils.GlideUtils;
import com.fly.caihong.utils.SizeUtils;
import com.fly.caihong.utils.ToastUtils;
import com.fly.caihong.view.PullRefreshRecyclerView;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.List;

public class HomeAuctionFragment extends BasePageListFragment {
    HomeAuctionItem homeAuctionItem;
    int defaultSize, allSize;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        homeAuctionItem = DataHandler.convertJsonData("home/auction", HomeAuctionItem.class);
        defaultSize = SizeUtils.dp2px(36);
        allSize = SizeUtils.dp2px(30);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        if (homeAuctionItem.getCategoryList() == null || homeAuctionItem.getAuctionTopicList() == null) {
            ToastUtils.showLong("拍卖数据和拍卖帖子数据为空");
            return;
        }
        View searchBar = LayoutInflater.from(getActivity()).inflate(R.layout.layout_search_bar, null);
        mAdapter.addHeaderView(searchBar);
        View attentionBar = LayoutInflater.from(getActivity()).inflate(R.layout.layout_attention_recycler_view, null);
        mAdapter.addHeaderView(attentionBar);
        searchBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        RecyclerView mRecyclerView = attentionBar.findViewById(R.id.rv_recycler);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 5));
        mRecyclerView.setAdapter(new BaseQuickAdapter<CategoryItem, BaseViewHolder>(R.layout.layout_attention_item, homeAuctionItem.getCategoryList()) {
            @Override
            protected void convert(BaseViewHolder helper, CategoryItem item) {
                helper.setText(R.id.tv_name, item.getText());
                ImageView ivProfile = helper.getView(R.id.iv_profile);
                FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) ivProfile.getLayoutParams();
                if ("全部分类".equals(item.getText())) {
                    lp.width = allSize;
                    lp.height = allSize;
                    ivProfile.setImageResource(R.drawable.ic_all_category);
                } else {
                    lp.width = defaultSize;
                    lp.height = defaultSize;
                    GlideUtils.loadSrcImage(ApUtils.getApp(), ivProfile, item.getPhoto());
                }
                helper.setOnClickListener(R.id.iv_profile, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
            }
        });
        View vv = LayoutInflater.from(getActivity()).inflate(R.layout.layout_banner, null, false);
        Banner banner = vv.findViewById(R.id.ban);
        List<BannerItem> banners = homeAuctionItem.getBanners();
        banner.setImages(banners);
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                GlideUtils.loadSrcImage(context, imageView, ((BannerItem) path).getImage());
            }
        });
        banner.start();
        mAdapter.addHeaderView(vv);
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                AuctionDetailActivity.launchActivity(getActivity(), homeAuctionItem.getBanners().get(position).getId());
            }
        });
    }

    @Override
    protected Pair<PullRefreshRecyclerView, BaseQuickAdapter> initPullRefreshView(View view) {
        if (homeAuctionItem == null) {
            homeAuctionItem = new HomeAuctionItem();
        }
        return new Pair(view.findViewById(R.id.pull_refresh_layout), new HomeAuctionAdapter(getActivity(), homeAuctionItem.getAuctionTopicList()));
    }

    @Override
    public void initData() {
        super.initData();
        dismissRefreshStatus();
    }

    @Override
    protected void loadPageData(int pageNum) {
        mPullRefreshView.loadMoreEnd();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_pull_refresh_layout;
    }

    @Override
    public void onRefresh() {
        dismissRefreshStatus();
    }

}
